package com.heatup.apt.api.controller.order.ordertype;

import org.springframework.stereotype.Component;

import com.heatup.apt.api.controller.order.ApplicationContextHolder;
import com.heatup.apt.api.controller.order.OrderVo;
import com.heatup.apt.api.controller.order.paytype.PayTypeAbstractFactory;

@Component
public class OrderProccesser {

	// 支付的参数Vo
	private OrderVo orderVo;
	// 支付方式工厂
	private PayTypeAbstractFactory payTypeFactory;
	// 支付类型工厂
	private OrderTypeAbstractFactory orderTypeAbstractFactory;

	public OrderProccesser() { }
	public OrderProccesser(OrderVo orderVo) {
		this.BuilderOrderProccesser(orderVo);
	}
	
	public void BuilderOrderProccesser(OrderVo orderVo){
		this.orderVo = orderVo;
		this.orderTypeAbstractFactory = ApplicationContextHolder.getBean(getServiceByPayType(orderVo.getOrderType()),
				OrderTypeAbstractFactory.class);
		this.payTypeFactory = ApplicationContextHolder.getBean(getServiceByPayType(orderVo.getPayType()),
				PayTypeAbstractFactory.class);
		orderTypeAbstractFactory.setPayTypeAbstractFactory(payTypeFactory);
	}
	
	public Boolean makeOrder() {
		if(this.orderVo == null) {
			throw new RuntimeException("请先执行BuilderOrderProccesser方法");
		}
		//处理参数
		boolean isCheck = orderTypeAbstractFactory.dealParam(this.orderVo);
		if(isCheck) {
			// 下单
			OrderVo orderTemp = orderTypeAbstractFactory.makeOrder(orderVo);
			if(orderTemp != null) {
				orderTypeAbstractFactory.updateStatus(orderTemp);
			}
		}
		return false;
	}

	public OrderTypeAbstractFactory getOrderTypeAbstractFactory() {
		return orderTypeAbstractFactory;
	}

	public PayTypeAbstractFactory getPayTypeFactory() {
		return payTypeFactory;
	}

	public OrderVo getOrderVo() {
		return orderVo;
	}

	public void setOrderVo(OrderVo orderVo) {
		this.orderVo = orderVo;
	}

	// 注入不同支付方式bean
	public String getServiceByPayType(Integer payType) {
		switch (payType) {
		case 1:
			return PayTye.wechat.getName();
		case 2:
			return PayTye.card.getName();
		case 3:
			return PayTye.coupon.getName();
		default:
			break;
		}
		return null;
	}

	// 注入不同支付方式bean
	public String getServiceByOrderType(Integer orderType) {
		switch (orderType) {
		case 1:
			return PayTye.team.getName();
		case 2:
			return PayTye.camp.getName();
		case 3:
			return PayTye.pri.getName();
		case 4:
			return PayTye.self.getName();
		case 11:
			return PayTye.meal.getName();
		default:
			break;
		}
		return null;
	}

	public enum PayTye {
		team("TeamOrderService", 1), camp("PrivateOrderService", 2), // 檬营
		pri("PrivateOrderService", 3), self("SelfOrderService", 4), meal("MealOrderService", 11),

		wechat("wechatPayService", 1), card("cardPayService", 2), coupon("couponService", 3);
		private String name;
		private int index;

		private PayTye(String name, int index) {
			this.name = name;
			this.index = index;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}
	}

	public static void main(String[] args) {
	}
}
